package com.test.lol;


import android.app.*;
import android.content.*;
import android.location.*;
import android.net.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;

public class HelloActivity extends Activity{
	
	private GridView gridview;
	private String latitude;
	private String longitude;
	private LocationManager locationManager;
	private LocationListener locationListener;
	
    /** Called when the activity is first created. */
    @Override  
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        String action = intent.getAction();
        
        // This action fires when a photo is shared using our share function.
        // Starts new activity ShareActivity for uploading a photo.
        // Uri is the path to the photo
        if(Intent.ACTION_SEND.equals(action)){    
        	Intent i = new Intent(HelloActivity.this, ShareActivity.class);     	
        	Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
        	i.putExtra("Uri", uri);
        	Log.v(this.getClass().getName(), "Before activity: " + uri);
        	startActivity(i);      
        	this.finish();
        }
        else {
        	getGps();
        }

    }
    
    public void getGps(){
        
    	// Acquire a reference to the system Location Manager
    	locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    	// Define a listener that responds to location updates
    	locationListener = new LocationListener() {
    	    public void onLocationChanged(Location location) {
    	      // Called when a new location is found by the network location provider.
    	      latitude = Double.toString(location.getLatitude());
    	      longitude = Double.toString(location.getLongitude());
    	      locationManager.removeUpdates(locationListener);
    	      getImages(latitude, longitude);
    	    }

    	    public void onStatusChanged(String provider, int status, Bundle extras) {}

    	    public void onProviderEnabled(String provider) {}

    	    public void onProviderDisabled(String provider) {}
    	  };

    	// Register the listener with the Location Manager to receive location updates
    	locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    	
    }   	
    	
    
    /*
     * Show images as thumbs in a given area
     */
    public void getImages(String latitude, String longitude){
    	
        // Gallery for showing photos.
    	// Uses the class ImageAdapter for achieving this.
        gridview = (GridView) findViewById(R.id.gridView);
        gridview.setAdapter(new ImageAdapter(this, latitude, longitude));
        
        // When a user clicks on an image in the gallery then ImageActivity starts
        // It's and activity for displaying photos in full scale & used for displaying comments
        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {            
                Intent i = new Intent(HelloActivity.this, ImageActivity.class);
                i.putExtra("ID", position);
                startActivity(i);
            }
        });
    	
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("Refresh");
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 0:     
            	getGps();
            	break;
        }
        return true;
    }
}


